import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

import {
	getUserInfo
} from '@/api'
import {
	removeToken
} from '@/utils/auth.js'

const modules = {
	state: {
		user: {},
		websock: null
	},
	mutations: {
		SET_USERINFO(state, data) {
			state.user = data
		},
		SET_WEBSOCK(state, data) {
			state.websock = data
		},
		CLOSE_WEBSOCK(state) {
			state.websock.close()
			state.websock = null
		}
	},
	actions: {
		GET_USERINFO(context) {
			getUserInfo().then(res => {
				context.commit('SET_USERINFO', res.data)
			})
		},
		LOGOUT(context) {
			context.commit('SET_USERINFO', {})
			context.commit('CLOSE_WEBSOCK')
			removeToken()
		}
	}
};

const store = new Vuex.Store({
	...modules
});
export default store;